import { apiClient } from '../api';
import type { 
    AddSegmentToTrackParams, 
    AddSegmentToTrackResponse, 
    SegmentType,
    AddVideoOperationsParams,
    AddVideoOperationsResponse,
    VideoOperationType
} from './types';

/**
 * 给特定轨道添加片段
 * @param params 添加片段的参数
 * @returns Promise<AddSegmentToTrackResponse>
 */
export async function addSegmentToTrack(params: AddSegmentToTrackParams): Promise<AddSegmentToTrackResponse> {
    // 验证必需参数
    if (!params.draft_id) {
        throw new Error('缺少draft_id参数，请提供草稿ID');
    }

    if (!params.track_id) {
        throw new Error('缺少track_id参数，请提供轨道ID');
    }

    if (!params.segment_type) {
        throw new Error('缺少segment_type参数，请提供片段类型');
    }

    // 验证片段类型是否有效
    const validSegmentTypes: SegmentType[] = ['video', 'audio', 'text', 'sticker', 'effect', 'filter'];
    if (!validSegmentTypes.includes(params.segment_type)) {
        throw new Error(`无效的片段类型: ${params.segment_type}。有效类型: ${validSegmentTypes.join(', ')}`);
    }

    // 构建请求数据
    const requestData = {
        track_id: params.track_id,
        segment_type: params.segment_type,
        start_time: params.start_time || 0,
        duration: params.duration || 1000000, // 默认1秒（微秒）
        segment_data: params.segment_data || {},
    };

    try {
        const response = await apiClient.post<AddSegmentToTrackResponse>(`/segments/add/${params.draft_id}/`, requestData);
        return response.data;
    } catch (error) {
        console.error('添加片段到轨道失败:', error);
        throw error;
    }
}

/**
 * 给特定片段的视频添加操作（新增模式）
 * @param params 添加视频操作的参数
 * @returns Promise<AddVideoOperationsResponse>
 */
export async function addVideoOperations(params: AddVideoOperationsParams): Promise<AddVideoOperationsResponse> {
    // 验证必需参数
    if (!params.draft_id) {
        throw new Error('缺少draft_id参数，请提供草稿ID');
    }

    if (!params.track_id) {
        throw new Error('缺少track_id参数，请提供轨道ID');
    }

    if (!params.segment_id) {
        throw new Error('缺少segment_id参数，请提供片段ID');
    }

    if (!params.operations || params.operations.length === 0) {
        throw new Error('缺少operations参数，请提供操作列表');
    }

    // 验证操作类型是否有效
    const validOperationTypes: VideoOperationType[] = [
        'animation', 
        'filter', 
        'effect', 
        'keyframe', 
        'mask', 
        'transition', 
        'background_filling'
    ];

    for (const operation of params.operations) {
        if (!validOperationTypes.includes(operation.operation_type)) {
            throw new Error(`无效的操作类型: ${operation.operation_type}。有效类型: ${validOperationTypes.join(', ')}`);
        }
    }

    // 构建请求数据
    const requestData = {
        operations: params.operations,
    };

    try {
        const response = await apiClient.post<AddVideoOperationsResponse>(
            `/drafts/${params.draft_id}/tracks/${params.track_id}/segments/${params.segment_id}/add-video-operations/`,
            requestData
        );
        return response.data;
    } catch (error) {
        console.error('添加视频操作失败:', error);
        throw error;
    }
}
